package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.AdvertiseMapper;
import com.fantastic.model.pojo.zjs.Advertise;
import com.fantastic.service.zjs.AdvertiseService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/2 21:37
 * @description: 广告信息表service层接口
 */
@Service
public class AdvertiseServiceImpl implements AdvertiseService {
    @Resource
    private AdvertiseMapper advertiseMapper;


    /**
     * 查询广告信息
     *
     * @param advertise
     * @return
     */
    @Override
    public List<Advertise> listAdvertises(Advertise advertise) {
        List<Advertise> advertises = advertiseMapper.listAdvertises(advertise);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), advertises);
        for (Advertise advert : advertises) {
            advert.setAdvertisePhoto(FilesUtils.getSlashParse(advert.getAdvertisePhoto()));
        }
        return advertises;
    }

    /**
     * 查询单个广告信息
     *
     * @param advertiseId
     * @return
     */
    @Override
    public Advertise getAdvertise(Integer advertiseId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), advertiseId);
        Advertise advertise = advertiseMapper.getAdvertise(advertiseId);
        advertise.setAdvertisePhoto(FilesUtils.getSlashParse(advertise.getAdvertisePhoto()));
        return advertise;
    }


    /**
     * 添加广告信息
     *
     * @param filePhoto
     * @param advertise
     * @return
     */
    @Override
    public Integer insertAdvertise(MultipartFile filePhoto, Advertise advertise) {
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), advertise.getAdvertiseTitle());
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), advertise.getAdvertiseState(), advertise.getAdvertiseType());
        //文件处理
        if (filePhoto != null && filePhoto.getSize() != 0) {
            //判断文件是否支持
            MultipartFileUtils.supportType(filePhoto.getOriginalFilename());
            //进行文件具体处理
            String relativePath = MultipartFileUtils.writeFile(filePhoto, "广告");
            advertise.setAdvertisePhoto(relativePath);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }
        return advertiseMapper.insertAdvertise(advertise);
    }

    /**
     * 修改广告信息
     *
     * @param filePhoto
     * @param advertise
     * @return
     */
    @Override
    public Integer updateAdvertise(MultipartFile filePhoto, Advertise advertise) {
        //判断id不为空
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), advertise.getAdvertiseId());
        //判断文件是否为空
        if (filePhoto != null && filePhoto.getSize() != 0) {
            MultipartFileUtils.supportType(filePhoto.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(filePhoto, "广告");
            //设置新路径给图片
            advertise.setAdvertisePhoto(relativePath);
            //查询这个数据拿到原来的图片的地址进行删除
            Advertise getAdvertise = getAdvertise(advertise.getAdvertiseId());
            //删除图片
            MultipartFileUtils.deleteFile(getAdvertise.getAdvertisePhoto());
        }
        return advertiseMapper.updateAdvertise(advertise);
    }

    /**
     * 删除广告信息
     *
     * @param advertiseId
     * @return
     */
    @Override
    public Integer deleteAdvertise(Integer advertiseId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR), advertiseId);
        return advertiseMapper.deleteAdvertise(advertiseId);
    }

}
